﻿@model SklepISI.Core.Models.ProductModel

@{
    ViewBag.Title = "Details";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<script>
    function removeAlerts() {
        $("form div.alert:first").alert('close');
    }

    function RefreshCartPreview() {
        $.post('@Url.Action("CartPreview","Cart")', function (data) {
            $("#cartPreview").html(data);
        });
    }

    var tmplSuccess = "<div class='alert alert-success fade in'><button type='button' class='close' data-dismiss='alert'>×</button><strong>Sukces!</strong> Dodawanie produktu do karty zakończone powodzeniem.</div>";
    var tmplFailed = "<div class='alert alert-error fade in'><button type='button' class='close' data-dismiss='alert'>×</button><strong>Porażka!</strong> Dodawanie produktu do karty zakończone niepowodzeniem.</div>";

    $(function () {
        $(".addToCart").on("click", function (e) {
            var prodId = $(this).attr("prodId");
            e.preventDefault();
            $.post('@Url.Action("AddToCart", "Cart")',
                {
                    id: prodId
                },
                function (data) {
                    if (data.Success == true) {
                        $("form.form-horizontal h3").after(tmplSuccess);
                        RefreshCartPreview();
                    }
                    else {
                        $("form.form-horizontal h3").after(tmplFailed);
                    }
                    setTimeout(removeAlerts, 2000);
                });

            return false;
        });
    });
</script>
<form class="form-horizontal well">
    <h3>@Model.Name</h3>
    @*<div class="controls-row">
        <b>Id produktu:</b>
        @Html.ValueFor(model => model.ProductId)
    </div>*@
    <div class="controls-row">
        <b>Cena:</b>
        @Html.DisplayFor(model => model.Price) zł
    </div>
    <div class="control-group" style="margin-top: 10px">
        <b>Opis:</b><br />
        @Html.DisplayTextFor(model => model.Description)
    </div>
    <div class="controls-row">
        @Html.ActionLink("Powrót", "List", new { categoryId = @Model.CategoryId }, new { @class = "btn" })
        @Html.ActionLink("Dodaj do koszyka", "AddToCart", "Cart", new { }, new { @class = "btn btn-primary addToCart", @prodId = @Model.ProductId })
    </div>
</form>
